package la.iok.hzsvn.lewin.movie.services;

import la.iok.hzsvn.lewin.movie.core.RoleEnum;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.exception.ErrorCode;
import la.iok.hzsvn.lewin.movie.core.model.SimpleOrgUnitVo;
import la.iok.hzsvn.share.annotations.NotNull;

import java.util.Collection;

public class OrgUnitIntegrationImpl implements OrgUnitIntegration{
    private final MovieServiceInvoker movieServiceInvoker;

    public OrgUnitIntegrationImpl(MovieServiceInvoker movieServiceInvoker) {
        this.movieServiceInvoker = movieServiceInvoker;
    }


    @Override
    public void userManageOrgUnits(@NotNull Token token, @NotNull Collection<Long> orgUnitIds) {
        if(token.hasAuthority(RoleEnum.Manager)){
            SimpleOrgUnitVo orgUnit = movieServiceInvoker.orgUnitInfo(token.getOrgUnitId());
            ErrorCode.ORG_UNIT_NOT_EXIST.assertNotNull(orgUnit, token.getOrgUnitId());
            if(orgUnit.franchiseeType()){
                orgUnitIds.addAll(movieServiceInvoker.childrenOrgUnitIds(token.getOrgUnitId()));
            }
        }
        if(token.getOrgUnitId() != null && token.getOrgUnitId() > 0){
            orgUnitIds.add(token.getOrgUnitId());
        }
    }
}
